using System;
using UnityEngine;

namespace ProceduralPrimitivesUtil
{
	public class DoubleSphere : PPBase
	{
		[Header("Basic parameters")]
		public float radius1 = 0.5f;

		public float radius2 = 0.3f;

		[Header("Segments")]
		public int segments = 24;

		[Header("Slice")]
		public bool sliceOn = true;

		public float sliceFrom = 90f;

		public float sliceTo = 360f;

		public SphereCutOption cutTpye;

		public float cutFrom;

		public float cutTo = 1f;

		[Header("Mapping Coordinates")]
		public bool generateMappingCoords = true;

		public bool realWorldMapSize;

		public Vector2 UVOffset = new Vector2(0f, 0f);

		public Vector2 UVTiling = new Vector2(1f, 1f);

		[Header("Others")]
		public bool flipNormals;

		public bool smooth = true;

		protected override void CreateMesh()
		{
			m_mesh.name = "DoubleSphere";
			radius1 = Mathf.Clamp(radius1, 1E-05f, 10000f);
			radius2 = Mathf.Clamp(radius2, 1E-05f, radius1);
			segments = Mathf.Clamp(segments, 4, 100);
			sliceFrom = Mathf.Clamp(sliceFrom, 0f, 360f);
			sliceTo = Mathf.Clamp(sliceTo, sliceFrom, 360f);
			cutFrom = Mathf.Clamp01(cutFrom);
			cutTo = Mathf.Clamp(cutTo, cutFrom, 1f);
			float num = (radius1 - radius2) / (radius1 * 2f);
			float num2 = (radius1 + radius2) / (radius1 * 2f);
			float num3 = Mathf.Clamp01((cutFrom - num) / (num2 - num));
			float num4 = Mathf.Clamp01((cutTo - num) / (num2 - num));
			float f = MathF.PI - Mathf.Acos((cutFrom - 0.5f) * 2f);
			float f2 = MathF.PI - Mathf.Acos((cutTo - 0.5f) * 2f);
			float f3 = MathF.PI - Mathf.Acos((num3 - 0.5f) * 2f);
			float f4 = MathF.PI - Mathf.Acos((num4 - 0.5f) * 2f);
			bool flag = cutTpye == SphereCutOption.HemiSphere;
			bool flag2 = cutTpye == SphereCutOption.SphericalSector;
			CreateSphere(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1, segments, segments / 2, sliceOn, sliceFrom, sliceTo, flag || flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
			if (flag2)
			{
				CreateSphere(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius2, segments, segments / 2, sliceOn, sliceFrom, sliceTo, flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
			}
			else
			{
				CreateSphere(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius2, segments, segments / 2, sliceOn, sliceFrom, sliceTo, flag, f3, f4, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
			}
			if (sliceOn)
			{
				Vector3 vector = new Vector3(Mathf.Sin(sliceFrom * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceFrom * (MathF.PI / 180f))) * radius1 * 0.5f;
				Vector3 vector2 = new Vector3(Mathf.Sin(sliceTo * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceTo * (MathF.PI / 180f))) * radius1 * 0.5f;
				CreateHemiRing(pivotOffset + Vector3.zero, m_rotation * Vector3.up, m_rotation * vector.normalized, radius1, radius2, segments / 2, 1, flag || flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, flag2);
				CreateHemiRing(pivotOffset + Vector3.zero, m_rotation * Vector3.up, m_rotation * vector2.normalized, radius1, radius2, segments / 2, 1, flag || flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, new Vector2(0f - UVTiling.x, UVTiling.y), !flipNormals, flag2);
			}
			Vector2 vector3 = new Vector2(Mathf.Sin(f), 0f - Mathf.Cos(f));
			Vector2 vector4 = new Vector2(Mathf.Sin(f2), 0f - Mathf.Cos(f2));
			if (flag)
			{
				Vector2 vector5 = new Vector2(Mathf.Sin(f3), 0f - Mathf.Cos(f3));
				Vector2 vector6 = new Vector2(Mathf.Sin(f4), 0f - Mathf.Cos(f4));
				CreateRing(pivotOffset + m_rotation * new Vector3(0f, vector3.y * radius1, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1 * vector3.x, radius2 * vector5.x, segments, 1, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
				CreateRing(pivotOffset + m_rotation * new Vector3(0f, vector4.y * radius1, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1 * vector4.x, radius2 * vector6.x, segments, 1, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			}
			if (flag2)
			{
				float num5 = vector3.y * (radius1 - radius2);
				float num6 = vector4.y * (radius1 - radius2);
				if (cutFrom > 0.5f)
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, vector3.y * radius2 + num5 * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num5, vector3.x * radius2, radius1 * vector3.x, segments, 1, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
				}
				else
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, vector3.y * radius2 + num5 * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, 0f - num5, radius1 * vector3.x, vector3.x * radius2, segments, 1, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
				}
				if (cutTo > 0.5f)
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, vector4.y * radius2 + num6 * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num6, vector4.x * radius2, radius1 * vector4.x, segments, 1, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
				}
				else
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, vector4.y * radius2 + num6 * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, 0f - num6, radius1 * vector4.x, vector4.x * radius2, segments, 1, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
				}
			}
		}
	}
}
